using UnityEngine;
using System.Collections;

public class CharacterLogic : MonoBehaviour
{

    private GameObject plane;
    private GameObject character;

    private bool isSpacePressed;


    private ArrayList obstacles = new ArrayList();
    //private Vector3 initialPlanePositionVector;

    private static float JUMP_HEIGHT = 0.9f;

    private float timer = 0;


    // Use this for initialization
    void Start()
    {
        plane = GameObject.Find("Ground");
        //initialPlanePositionVector = plane.transform.position;
        character = GameObject.Find("Constructor");
        character.animation.Play("run");
        isSpacePressed = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (timer > 0)
        {
            timer -= Time.deltaTime;
        }
        if (!character.animation.IsPlaying("jump_pose") && isSpacePressed == false)
        {
            character.animation.Blend("run");
        }

        //Jump
        if (!isSpacePressed && Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("Jump!");
            isSpacePressed = true;
            character.animation.Stop("run");
            character.animation["jump_pose"].normalizedTime = 0;
            character.animation["jump_pose"].speed = 1.0f;
            character.animation.wrapMode = WrapMode.Once;
            character.animation.Play("jump_pose");

            character.transform.position = new Vector3(character.transform.position.x, character.transform.position.y + JUMP_HEIGHT, character.transform.position.z);

            timer = 0.6f;
        }

        if (timer <= 0 && !character.animation.IsPlaying("jump_pose") && isSpacePressed)
        {
            character.animation["jump_pose"].normalizedTime = 1;
            character.animation["jump_pose"].speed = -1.0f;
            character.animation.Play("jump_pose");
            character.transform.position = new Vector3(character.transform.position.x, character.transform.position.y - JUMP_HEIGHT, character.transform.position.z);
            isSpacePressed = false;
            timer = 0;
        }

        //Left
        if (Input.GetKeyDown(KeyCode.A) && character.transform.position.x != - 0.6f)
        {
            character.transform.position = new Vector3(character.transform.position.x - 0.6f, character.transform.position.y, character.transform.position.z);
        }
        //Right
        if (Input.GetKeyDown(KeyCode.D) && character.transform.position.x != 0.6f)
        {
            character.transform.position = new Vector3(character.transform.position.x + 0.6f, character.transform.position.y, character.transform.position.z);
        }
    }

    void OnCollisionEnter(Collision collision)
    {
        Debug.Log("OnCollisionEnter " + collision.gameObject.name);
    }

    void OnTriggerEnter(Collider collider)
    {
        Debug.Log("OnTriggerEnter " + collider.gameObject.name);
    }
}
